package com.platform.example.service;

import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.bean.Message;
import com.platform.example.dao.PersonRepository;
import com.platform.example.entity.Person;
import com.platform.framework.service.BaseServiceImpl;

/**
 * 
  * 集成BaseServiceImpl，拥有原生sql的能力
  * @author 王求卿[wangqiuqing] Tel:18101037995
  * @createDate 2017年8月8日 上午8:52:28
 */
@Service
public class PersonService extends BaseServiceImpl {

	@Autowired
	PersonRepository personRepository;
	
	/**
	 * 
	  * demo保存	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param person
	  * @return 保存的ID
	  * @createDate 2017年8月8日 上午8:57:38
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional
	public String savePerson(Person person) {
		try {
			person.setId(this.getUUID());
			person.setBirthday(new Date());
			personRepository.save(person);
			this.getLogDao().save(this.getOperationLog(OPERATE_TYPE_ADD, "测试person表",
					"添加peson【" + JSONObject.toJSONString(person) + "】"));
		} catch (Exception e) {
			return new Message(Message.ERROR, "添加测试person表失败").toJson();
		}
		return new Message(Message.OK, person.getId()).toJson();
	}
	
	/**
	 * 
	  * 删除 dmeo 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param ids
	  * @return
	  * @createDate 2017年8月8日 上午8:58:19
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional
	public String deletePerson(String ids) {
		String[] idArr = ids.split(",");
		int lens = idArr.length;
		try {
			for (int i = 0; i < lens; i++) {
				Person person = personRepository.findOne(idArr[i]);
				this.getLogDao().save(this.getLogDao().save(this.getOperationLog(OPERATE_TYPE_DELETE, "系统服务器表",
						"删除系统服务器【" + JSONObject.toJSONString(person) + "】")));
				personRepository.delete(person);
			}
		} catch (Exception e) {
			return new Message(Message.ERROR, "删除测试person表失败").toJson();
		}
		return new Message(Message.OK, lens + "").toJson();
	}

	/**
	 * 
	  * 更新demo 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param person
	  * @return
	  * @createDate 2017年8月8日 上午8:58:30
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional
	public String updatePerson(Person person) {
		try {
			Person person_origin = personRepository.findOne(person.getId());
			person.setBirthday(new Date());
			this.getLogDao()
					.save(this.getOperationLog(OPERATE_TYPE_UPDATE, "测试person表",
							"对id为[" + person_origin.getId() + "]的机构进行如下修改：原测试person表【"
									+ JSONObject.toJSONString(person_origin) + "】,新测试person表:【"
									+ JSONObject.toJSONString(person) + "】"));
			personRepository.save(person);
		} catch (Exception e) {
			return new Message(Message.ERROR, "更新测试person表失败").toJson();
		}
		return new Message(Message.OK, person.getId()).toJson();
	}

	/**
	 * 
	  * 查询demo 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param searchCtx
	  * @param pageable
	  * @return
	  * @throws Exception
	  * @createDate 2017年8月8日 上午8:58:38
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Page<Person> findPersonPage(String searchCtx, Pageable pageable) throws Exception {

		if (StringUtils.isEmpty(searchCtx)) {
			searchCtx = "";
		}
		return super.getDao().findEntityPagingBySql("select * from t_person where name like ? or age like ?",  new Object[] {'%'+searchCtx+'%','%'+searchCtx+'%'}, pageable, Person.class);
	}

}